package com.magina.nety.config;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.lang.UUID;
import com.magina.nety.redisCache.PageRedis;
import com.magina.nety.redisCache.BaseSearchModel;
import org.assertj.core.util.Lists;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

@Service
public class TestCacheService {


    @Cacheable(value = "mycache",key = "#id")
    public List<String> findAll(String id){
        System.out.println("no cache");
        return Arrays.asList("1","123");
    }

    @PageRedis(key = "#searchModel.toString()")
    public Page<TestDemo> testPageRedis(BaseSearchModel searchModel , PageRequest pageReques){
        System.out.println("no cache");
        List<TestDemo> demos = Lists.newArrayList();
        for (int i = 0; i < 100; i++) {
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(new Date());
            calendar.add(Calendar.DAY_OF_YEAR,i);
            calendar.getTime();
            String dateTime  = DateUtil.format(calendar.getTime(),"yyyy-MM-dd");
            calendar.add(Calendar.DAY_OF_YEAR,1);
            String afterTime  = DateUtil.format(calendar.getTime(),"yyyy-MM-dd");
            TestDemo testDemo = TestDemo.builder().id(UUID.randomUUID().toString())
                    .date(dateTime + " - " +afterTime)
                    .name("test" + i)
                    .code(i+"code").build();
            demos.add(testDemo);
        }
        return new PageImpl<>(demos,pageReques,demos.size());
    }


}
